package top.milkbox.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import top.milkbox.domain.Post;
import top.milkbox.domain.PostComment;
import top.milkbox.service.PostCommentService;
import top.milkbox.mapper.PostCommentMapper;
import org.springframework.stereotype.Service;
import top.milkbox.service.PostService;
import top.milkbox.service.UserService;
import top.milkbox.utils.GuoUtilsDate;

/**
* @author xiaoguo
* @description 针对表【tb_post_comment】的数据库操作Service实现
* @createDate 2022-11-02 21:36:24
*/
@Service
public class PostCommentServiceImpl extends ServiceImpl<PostCommentMapper, PostComment>
    implements PostCommentService{

    @Autowired
    private PostCommentMapper postCommentMapper;

    @Autowired
    private PostService postService;

    @Autowired
    private UserService userService;

    @Override
    public boolean addComment(String token, Integer postId, String content) {
        // 校验帖子是否存在
        if (postId == null || postService.getById(postId) == null) {
            log.error("postId（" + postId + "）不存在");
            return false;
        }

        PostComment postComment = new PostComment();
        postComment.setPostId(postId);
        postComment.setUserId(userService.getUserFromRedis(token).getUserId());
        postComment.setContent(content);
        postComment.setTime(GuoUtilsDate.getDateNow());
        log.debug("即将插入的数据：" + postComment.toString());

        int isInsert = postCommentMapper.insert(postComment);
        if (isInsert == 1) {
            return true;
        } else {
            log.error("数据库异常");
            return false;
        }
    }
}




